package game;

import java.io.IOException;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

import entities.Player;

public class Game extends BasicGameState {
	
	boolean left, right = true;
	
	int x, y;
	int time;
	
	Player player;
	Input input;
	Image pi, gi;
	
	public int getID() {
		return Main.GAMESTATE;
	}

	public void init(GameContainer gc, StateBasedGame sb) throws SlickException {
		x = 0;
		y = 0;
		
		pi = new Image("res/img/playerr.png");
		gi = new Image("res/img/grass.png");
		
		try { 
			player = new Player(pi);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		input = gc.getInput();
	}

	public void render(GameContainer gc, StateBasedGame sb, Graphics g) throws SlickException {
		for (int x = 0; x < 20; x++) {
			for (int y = 0; y < 15; y++) {
				g.drawImage(gi, x * 32, y * 32);
			}
		}
		
		if (right && !left) {
			player.draw(g, x, y);
		} else if (left && !right) {
			player.draw(g, x, y);
		}
	}

	public void update(GameContainer gc, StateBasedGame sb, int delta) throws SlickException {		
		if (input.isKeyPressed(Input.KEY_LEFT)) { 
			left = true;
			right = false;
			x -= 32; 
		}
		else if (input.isKeyPressed(Input.KEY_RIGHT)) { 
			right = true;
			left = false;
			x += 32; 
		}
		else if (input.isKeyPressed(Input.KEY_UP)) {
			y -= 32; 
		}
		else if (input.isKeyPressed(Input.KEY_DOWN)) {
			y += 32; 
		}
			
		if (x < 0) { x = 0; }
		if (x > 608) { x = 608; }
		if (y < 0) { y = 0; }
		if (y > 448) { y = 448; }
	}
}